/*
 * Created on 2003-6-11
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package com.mes.common.usermanagement;

import java.io.IOException;

import org.apache.log4j.Logger;
/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class MMPath {
	private static Logger logger=Logger.getLogger(MMPath.class.getName());
    public static final String FILE_SEPARATOR = System.getProperty("file.separator");

    private static String mmHome;

	public MMPath(){

	}
	
	//获取项目物理根目录绝对路径
	public static String getWebAppRootPath(){
	String result = MMPath.class.getResource("MMPath.class").toString();
	logger.info("the result is + "+result);
	int index = result.indexOf("WEB-INF");
	if (index == -1) {
	index = result.indexOf("bin");
	}
	result = result.substring(0, index);
	 if(result.startsWith("jar")){ // 当class文件在jar文件中时，返回"jar:file:/F:/ ..."样的路径
			result = result.substring(10);
			logger.info("the jar result is + "+result);
	 } else if (result.startsWith("zip")) { // 当class文件在war中时，返回"zip:D:/ ..."样的路径
	result = result.substring(4);
	logger.info("the zip result is + "+result);
	} else if (result.startsWith("file://")) { // 当class文件在class文件中时，返回"file:/F:/ ..."样的路径 
	result = result.substring(6);
	} else if (result.startsWith("file:/")) { // 当class文件在class文件中时，返回"file:/F:/ ..."样的路径 
		result = result.substring(5);//实用于unix
	}
	
	if (result.endsWith("/")) result = result.substring(0, result.length() - 1);// 不包含最后的"/"
	result = result.replace("%20", " "); //Window下替换空格
	logger.info("the last result is + "+result);
	return result;
	}
	
	
	public static String getMMHome()
	    {
		//logger.info("the File_separator is "+FILE_SEPARATOR);
	        if(mmHome == null)
	        {
//	        	String filePath = MMPath.class.getProtectionDomain().getCodeSource().getLocation().getPath();
//	        	int index = filePath.indexOf("WEB-INF/classes");
//	        	filePath = filePath.substring(0, index);
//	        	mmHome = filePath + "mm_home/";
	        	mmHome=getWebAppRootPath()+"/"+"mm_home/";
	            if(mmHome == null)
	            {
	            	try{
	            		Environment env = new Environment();
		                mmHome = env.getProperty("MM_HOME");
		                logger.info("++++++++the retrieved mm_home is+++++++ "+mmHome);
	            	}catch(Exception ex){
	            		ex.printStackTrace();
	            	}
	            }
//	            String syspath = mmHome + FILE_SEPARATOR;
	        }
	        if(mmHome == null || mmHome.equals("")){
	        	try{
	        		throw new IOException("Can not read 'MM_HOME'!");
	        	}catch(Exception ex){

	        	}
	        }

	        if(!mmHome.equals(null))
	          return mmHome;
	        else return "";

	    }
}
